home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / src-glut / win32_glx.c < prev    next >
C/C++ Source or Header  |  1998-12-15  |  7KB  |  259 lines

  1.  
  2. /* Copyright (c) Nate Robins, 1997. */
  3.  
  4. /* This program is freely distributable without licensing fees
  5.    and is provided without guarantee or warrantee expressed or
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. #include <stdio.h>
  9. #include <windows.h> /* needed when built w/o mesa gl.h, e.g.: using DevStudio gl.h */
  10. #include <gl/gl.h>
  11. #include "win32_glx.h"
  12.  
  13. /* global current HDC */
  14. extern HDC XHDC;
  15. extern HWND XHWND;
  16.  
  17. GLXContext
  18. glXCreateContext(Display * display, XVisualInfo * visinfo,
  19.   GLXContext share, Bool direct)
  20. {
  21.   /* KLUDGE: GLX really expects a display pointer to be passed
  22.      in as the first parameter, but Win32 needs an HDC instead,
  23.      so BE SURE that the global XHDC is set before calling this
  24.      routine. */
  25.   HGLRC context;
  26.  
  27.   context = wglCreateContext(XHDC);
  28.  
  29. #if 0
  30.   /* XXX GLUT doesn't support it now, so don't worry about display list
  31.      and texture object sharing. */
  32.   if (share) {
  33.     wglShareLists(share, context);
  34.   }
  35. #endif
  36.  
  37.   /* Since direct rendering is implicit, the direct flag is
  38.      ignored. */
  39.  
  40.   return context;
  41. }
  42.  
  43. int
  44. glXGetConfig(Display * display, XVisualInfo * visual, int attrib, int *value)
  45. {
  46.   if (!visual)
  47.     return GLX_BAD_VISUAL;
  48.  
  49.   switch (attrib) {
  50.   case GLX_USE_GL:
  51.     if (visual->dwFlags & (PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW)) {
  52.       /* XXX Brad's Matrix Millenium II has problems creating
  53.          color index windows in 24-bit mode (lead to GDI crash)
  54.          and 32-bit mode (lead to black window).  The cColorBits
  55.          filed of the PIXELFORMATDESCRIPTOR returned claims to
  56.          have 24 and 32 bits respectively of color indices. 2^24
  57.          and 2^32 are ridiculously huge writable colormaps.
  58.          Assume that if we get back a color index
  59.          PIXELFORMATDESCRIPTOR with 24 or more bits, the
  60.          PIXELFORMATDESCRIPTOR doesn't really work and skip it.
  61.          -mjk */
  62.       if (visual->iPixelType == PFD_TYPE_COLORINDEX
  63.         && visual->cColorBits >= 24) {
  64.         *value = 0;
  65.       } else {
  66.     *value = 1;
  67.       }
  68.     } else {
  69.       *value = 0;
  70.     }
  71.     break;
  72.   case GLX_BUFFER_SIZE:
  73.     /* KLUDGE: if we're RGBA, return the number of bits/pixel,
  74.        otherwise, return 8 (we guessed at 256 colors in CI
  75.        mode). */
  76.     if (visual->iPixelType == PFD_TYPE_RGBA)
  77.       *value = visual->cColorBits;
  78.     else
  79.       *value = 8;
  80.     break;
  81.   case GLX_LEVEL:
  82.     /* The bReserved flag of the pfd contains the
  83.        overlay/underlay info. */
  84.     *value = visual->bReserved;
  85.     break;
  86.   case GLX_RGBA:
  87.     *value = visual->iPixelType == PFD_TYPE_RGBA;
  88.     break;
  89.   case GLX_DOUBLEBUFFER:
  90.     *value = visual->dwFlags & PFD_DOUBLEBUFFER;
  91.     break;
  92.   case GLX_STEREO:
  93.     *value = visual->dwFlags & PFD_STEREO;
  94.     break;
  95.   case GLX_AUX_BUFFERS:
  96.     *value = visual->cAuxBuffers;
  97.     break;
  98.   case GLX_RED_SIZE:
  99.     *value = visual->cRedBits;
  100.     break;
  101.   case GLX_GREEN_SIZE:
  102.     *value = visual->cGreenBits;
  103.     break;
  104.   case GLX_BLUE_SIZE:
  105.     *value = visual->cBlueBits;
  106.     break;
  107.   case GLX_ALPHA_SIZE:
  108.     *value = visual->cAlphaBits;
  109.     break;
  110.   case GLX_DEPTH_SIZE:
  111.     *value = visual->cDepthBits;
  112.     break;
  113.   case GLX_STENCIL_SIZE:
  114.     *value = visual->cStencilBits;
  115.     break;
  116.   case GLX_ACCUM_RED_SIZE:
  117.     *value = visual->cAccumRedBits;
  118.     break;
  119.   case GLX_ACCUM_GREEN_SIZE:
  120.     *value = visual->cAccumGreenBits;
  121.     break;
  122.   case GLX_ACCUM_BLUE_SIZE:
  123.     *value = visual->cAccumBlueBits;
  124.     break;
  125.   case GLX_ACCUM_ALPHA_SIZE:
  126.     *value = visual->cAccumAlphaBits;
  127.     break;
  128.   default:
  129.     return GLX_BAD_ATTRIB;
  130.   }
  131.   return 0;
  132. }
  133.  
  134. XVisualInfo *
  135. glXChooseVisual(Display * display, int screen, int *attribList)
  136. {
  137.   /* KLUDGE: since we need the HDC, MAKE SURE to set XHDC
  138.      before calling this routine. */
  139.  
  140.   int *p = attribList;
  141.   int pf;
  142.   PIXELFORMATDESCRIPTOR pfd;
  143.   PIXELFORMATDESCRIPTOR *match = NULL;
  144.   int stereo = 0;
  145.  
  146.   /* Avoid seg-faults. */
  147.   if (!p)
  148.     return NULL;
  149.  
  150.   memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
  151.   pfd.nSize = (sizeof(PIXELFORMATDESCRIPTOR));
  152.   pfd.nVersion = 1;
  153.  
  154.   /* Defaults. */
  155.   pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
  156.   pfd.iPixelType = PFD_TYPE_COLORINDEX;
  157.   pfd.cColorBits = 32;
  158.   pfd.cDepthBits = 0;
  159.  
  160.   while (*p) {
  161.     switch (*p) {
  162.     case GLX_USE_GL:
  163.       pfd.dwFlags |= PFD_SUPPORT_OPENGL;
  164.       break;
  165.     case GLX_BUFFER_SIZE:
  166.       pfd.cColorBits = *(++p);
  167.       break;
  168.     case GLX_LEVEL:
  169.       /* the bReserved flag of the pfd contains the
  170.          overlay/underlay info. */
  171.       pfd.bReserved = *(++p);
  172.       break;
  173.     case GLX_RGBA:
  174.       pfd.iPixelType = PFD_TYPE_RGBA;
  175.       break;
  176.     case GLX_DOUBLEBUFFER:
  177.       pfd.dwFlags |= PFD_DOUBLEBUFFER;
  178.       break;
  179.     case GLX_STEREO:
  180.       stereo = 1;
  181.       pfd.dwFlags |= PFD_STEREO;
  182.       break;
  183.     case GLX_AUX_BUFFERS:
  184.       pfd.cAuxBuffers = *(++p);
  185.       break;
  186.     case GLX_RED_SIZE:
  187.       pfd.cRedBits = 8; /* Try to get the maximum. */
  188.       ++p;
  189.       break;
  190.     case GLX_GREEN_SIZE:
  191.       pfd.cGreenBits = 8;
  192.       ++p;
  193.       break;
  194.     case GLX_BLUE_SIZE:
  195.       pfd.cBlueBits = 8;
  196.       ++p;
  197.       break;
  198.     case GLX_ALPHA_SIZE:
  199.       pfd.cAlphaBits = 8;
  200.       ++p;
  201.       break;
  202.     case GLX_DEPTH_SIZE:
  203.       pfd.cDepthBits = 32;
  204.       ++p;
  205.       break;
  206.     case GLX_STENCIL_SIZE:
  207.       pfd.cStencilBits = *(++p);
  208.       break;
  209.     case GLX_ACCUM_RED_SIZE:
  210.     case GLX_ACCUM_GREEN_SIZE:
  211.     case GLX_ACCUM_BLUE_SIZE:
  212.     case GLX_ACCUM_ALPHA_SIZE:
  213.       /* I believe that WGL only used the cAccumRedBits,
  214.      cAccumBlueBits, cAccumGreenBits, and cAccumAlphaBits fields
  215.      when returning info about the accumulation buffer precision.
  216.      Only cAccumBits is used for requesting an accumulation
  217.      buffer. */
  218.       pfd.cAccumBits = 1;
  219.       ++p;
  220.       break;
  221.     }
  222.     ++p;
  223.   }
  224.  
  225.   /* Let Win32 choose one for us. */
  226.   pf = ChoosePixelFormat(XHDC, &pfd);
  227.   if (pf > 0) {
  228.     match = (PIXELFORMATDESCRIPTOR *) malloc(sizeof(PIXELFORMATDESCRIPTOR));
  229.     DescribePixelFormat(XHDC, pf, sizeof(PIXELFORMATDESCRIPTOR), match);
  230.  
  231.     /* ChoosePixelFormat is dumb in that it will return a pixel
  232.        format that doesn't have stereo even if it was requested
  233.        so we need to make sure that if stereo was selected, we
  234.        got it. */
  235.     if (stereo) {
  236.       if (!(match->dwFlags & PFD_STEREO)) {
  237.         free(match);
  238.     return NULL;
  239.       }
  240.     }
  241.     /* XXX Brad's Matrix Millenium II has problems creating
  242.        color index windows in 24-bit mode (lead to GDI crash)
  243.        and 32-bit mode (lead to black window).  The cColorBits
  244.        filed of the PIXELFORMATDESCRIPTOR returned claims to
  245.        have 24 and 32 bits respectively of color indices. 2^24
  246.        and 2^32 are ridiculously huge writable colormaps.
  247.        Assume that if we get back a color index
  248.        PIXELFORMATDESCRIPTOR with 24 or more bits, the
  249.        PIXELFORMATDESCRIPTOR doesn't really work and skip it.
  250.        -mjk */
  251.     if (match->iPixelType == PFD_TYPE_COLORINDEX
  252.       && match->cColorBits >= 24) {
  253.       free(match);
  254.       return NULL;
  255.     }
  256.   }
  257.   return match;
  258. }
  259.